<?php


namespace app\adminbackstage\controller;


use app\adminbackstage\service\JurisdictionService;
use app\adminbackstage\service\OrderService;
use app\adminbackstage\service\UserManagementService;
use app\BaseController;
use app\Response;
use think\facade\Request;

class Management extends BaseController
{
    public function userList(){
        $page = Request::param('page');
        $num = Request::param('num');
        $type = Request::param('type');//类型 1后台管理员 2后台普通管理员  11驿站 15快递员  20 商家
        $m_id = Request::param('m_id');
        $uid = Request::param('uid');
        $enable = Request::param('enable');
        $start = Request::param('start');
        $end = Request::param('end');
        $phone = Request::param('phone');
        $spread =  Request::param("spread");
        $channel =  Request::param("channel");
        $username =  Request::param("username");
        $prev_username =  Request::param("prev_username");
        $prev_phone =  Request::param("prev_phone");
        $wit_pro =  Request::param("wit_pro");
        $taobao = Request::param("taobao");
        $suspicious = Request::param("suspicious");
        $jd = Request::param("jd");
        $pdd = Request::param("pdd");
        $rid =  Request::param("rid");
        $a_uid = Request::param('a_uid');//管理员ID
        $order_tail_num = Request::param("order_tail_num");
        $wx_account = Request::param("wx_account");
        $res = JurisdictionService::checkAdminPower($a_uid);
        if(!empty($res)){
            return $res;
        }
        $data = [
            "page" => $page,
            "num" => $num,
            "type" => $type,
            "uid" => $uid,
            "enable" => $enable,
            "start" => $start,
            "end" => $end,
            "phone" => $phone,
            "spread" => $spread,
            "channel" => $channel,
            "username" => $username,
            "wit_pro" => $wit_pro,
            "taobao" => $taobao,
            "suspicious" => $suspicious,
            "jd" => $jd,
            "pdd" => $pdd,
            "rid" => $rid,
            "order_tail_num" => $order_tail_num,
            "prev_username" => $prev_username,
            "prev_phone" => $prev_phone,
            "wx_account"=>$wx_account
        ];
        list($list,$count) = UserManagementService::userListService($data);
        return Response::wrapData($list,$count);
    }


    //添加管理员
    public function add_user(){
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $username = Request::param('user');
        $pw = Request::param('pw');
        $phone = Request::param('phone');
        $res = UserManagementService::addUserService($username,$pw,$phone);
        return $res;
    }


    //人员修改
    public function userEdit(){

        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $uid = Request::param('uid');
        $enable = Request::param('enable',1);
        $phone = Request::param('phone');
        $pw = Request::param('pw');
        $res = UserManagementService::editUserService($uid,$enable,$phone,$pw);
        return $res;
    }


    public function userInfoEdit(){

        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $uid = Request::param('uid');
        $enable = Request::param('enable',1);
        $enable_type = Request::param('enable_type',"");
        $phone = Request::param('phone');
        $wx_account = Request::param('wx_account');
        $remarks = Request::param('remarks');
        $res = UserManagementService::editUserInfoService($uid,$enable,$phone,$wx_account,$enable_type,$remarks);
        return $res;
    }

    //用户禁用启用操作
    public function userDisable(){
        $page = Request::param('page');
        $num = Request::param('num');
        $start = Request::param('start');
        $end = Request::param('end');
        $uid = Request::param('uid');
        $operation = Request::param('operation');
        $data = [
            "page" => $page,
            "num" => $num,
            "start" => $start,
            "end" => $end,
            "operation" => $operation,
            "uid" => $uid
        ];
        list($list,$count) = UserManagementService::userDisableService($data);
        return Response::wrapData($list,$count);

    }

    public function interdictUser(){
        $a_uid = Request::param("a_uid","");
        $uid = Request::param("uid","");

        $enable =  Request::param("enable","");
        $remarks = Request::param("remarks","");
        $personnel = Request::param('personnel');
        if($enable == -1||$enable == 0){
            $enable = 0;
        }
        if(empty($uid)){
            return Response::wrapError(0,"请选择要操作的用户!");
        }
        $res = UserManagementService::interdictUserService($uid,$enable,$remarks,$personnel);
        return $res;
    }



    public static function merchantView(){
        $a_uid = Request::param("a_uid","");
        $uid = Request::param("uid","");

        $res = UserManagementService::merchantViewService($uid);
        return $res;
    }

    public function binfPrevUser(){
        $uid = Request::param("uid","");
        $prev_id = Request::param("prev_id","");
        $prev_code = Request::param("prev_code","");
        $phone = Request::param("phone","");
        if(!empty($prev_code)){
            $userNumber = intval($prev_code);
            $prev_id = bcdiv(bcsub($userNumber,3,0),2,0);;
        }
        $res = UserManagementService::editUserInfoService($uid,"","","","","",$prev_id);
        return $res;
    }
}